package cn.wmw.inactive.service.impl;

import cn.wmw.inactive.domain.User;
import cn.wmw.inactive.enums.ErrorMsg;
import cn.wmw.inactive.mapper.UserMapper;
import cn.wmw.inactive.service.UserService;
import cn.wmw.inactive.vo.PageVo;
import cn.wmw.inactive.vo.ResultVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.util.List;



@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserMapper userMapper;

    /**
     * 查询一个用户的公开信息
     * @param id
     * @return
     */
    public User getUser(Long id){
        return userMapper.selectByPrimaryKey(id);
    }

    /**
     * 登录，安全问题未解决
     * @param accountNumber
     * @param userPassword
     * @return
     */
    public ResultVo userLogin(String accountNumber, String userPassword, HttpServletResponse response){
        User user = userMapper.userLogin(accountNumber, userPassword);
        if (null == user) {
            return ResultVo.fail(ErrorMsg.EMAIL_LOGIN_ERROR);
        }
        // 用户名或者密码为空
        if(accountNumber.equals("") || userPassword.equals("")){
            return ResultVo.fail(ErrorMsg.EMAIL_LOGIN_ERROR);
        }
        // 手机号长度不足11位
        if(user.getAccountNumber().length() != 11){
            return ResultVo.fail(ErrorMsg.EMAIL_LOGIN_ERROR);
        }
        if(user.getUserStatus()!=null&&user.getUserStatus().equals((byte) 1)){
            return ResultVo.fail(ErrorMsg.ACCOUNT_Ban);
        }
        Cookie cookie = new Cookie("shUserId", String.valueOf(user.getId()));
//        cookie.setMaxAge(60 * 60 * 24 * 30);
        cookie.setPath("/");
        cookie.setHttpOnly(false);
        response.addCookie(cookie);
        return ResultVo.success(user);
    }

    /**
     *注册
     * @param user
     * @return
     */
    public boolean userSignIn(User user){
        return userMapper.insert(user) == 1;
    }

    /**
     *修改用户公开信息，未验证用户身份
     * @param user
     * @return
     */
    public boolean updateUserInfo(User user){
        return userMapper.updateByPrimaryKeySelective(user)==1;
    }

    /**
     * 修改密码
     * @param newPassword
     * @param oldPassword
     * @param id
     * @return
     */
    public boolean updatePassword(String newPassword, String oldPassword,Long id){
        return userMapper.updatePassword(newPassword,oldPassword,id)==1;
    }

    public PageVo<User> getUserByStatus(int status,int page ,int nums){
        List<User> list;
        int count=0;
        if(status==0){
            count=userMapper.countNormalUser();
            list=userMapper.getNormalUser((page-1)*nums, nums);
        }else {
            count=userMapper.countBanUser();
            list=userMapper.getBanUser((page-1)*nums, nums);
        }
        return new PageVo<>(list,count);
    }


    /**
     * 通过用户的姓名查询用户的id
     *
     * */
	@Override
	public Long getUserId(String nickname) {
		return userMapper.selectByUserName(nickname);
	}


	/**
     * 通过用户账号查询用户信息
     *
     * @return*/
    @Override
    public PageVo<User> getUserByNumber(String searchValue, int mode) {
        List<User> list;

//        System.out.println("-------------------" + 123 + "-----------------------");
        list=userMapper.getUserByNumber(searchValue, mode-1);
//
//        System.out.println("-------------------" + 456 + "-----------------------");
//        for (User i: list) {
//            System.out.println(i.getNickname() + " " + i.getUserStatus());
//        }
//        System.out.println("-------------------" + 789 + "-----------------------");
        return new PageVo<>(list, 1);
    }

}
